package com.taotao.service;

import com.taotao.base.BaseService;
import com.taotao.container.SWRegisterContainer;
import com.taotao.entity.ServiceInstance;
import com.taotao.rq.parameter.ReqInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Map;

/**
 * @author wangjin
 * @title: RegisterService
 * @projectName sw-springcloud
 * @description:
 * @date 2021/10/28 21:22
 */
@RequestMapping("/sw")
@RestController
public class RegisterService extends BaseService {
    @Autowired
    private SWRegisterContainer swRegisterContainer;

    /**
     * 服务接口注册
     * 传递参数? 服务名称和服务ip和端口
     *
     * @return
     */
    @PostMapping("/register")
    public ResponseEntity register(@RequestBody ReqInfoDto reqInfoDto) {
        //将服务地址放在容器
        return setResult(swRegisterContainer.put(reqInfoDto));
    }

    /**
     * 服务查询
     *
     * @return
     */
    @GetMapping("/activeService")
    public Map<String, HashSet<ServiceInstance>> activeService() {
        return swRegisterContainer.getMapAddres();
    }

    /**
     * 服务移除
     *
     * @param reqInfoDto
     * @return
     */
    @PostMapping("/remove")
    public ResponseEntity remove(@RequestBody ReqInfoDto reqInfoDto) {
        return setResult(swRegisterContainer.remove(reqInfoDto) != null);
    }

    /**
     * 服务续约
     *
     * @param reqInfoDto
     * @return
     */
    @PostMapping("/renewal")
    public ResponseEntity renewal(@RequestBody ReqInfoDto reqInfoDto) {
        return setResult(swRegisterContainer.renewal(reqInfoDto));
    }


}
